package com.bzkj.web.controller.stockmanagement;

import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.core.page.TableDataInfoInvention;
import com.bzkj.common.utils.poi.ExcelUtil;
import com.bzkj.document.domain.BasicQueryDTO;
import com.bzkj.document.utils.common.DataProcessUtils;
import com.bzkj.inventory.domain.InventoryDiscrepancyDetail;
import com.bzkj.inventory.service.InventoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName NccInventoryController
 * @Description TODO
 * @Author C1309
 * @Date 2025/5/13 13:25
 */
@RestController
@RequestMapping("/api/inventory")
public class NccInventoryController extends BaseController {
    @Resource
    private InventoryService inventoryService;

    /**
     * 获取NCC库存列表
     *
     * @param warehouseId 仓库主键
     * @return 查询结果
     */
    @GetMapping("/getInventoryList")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:list')")
    public TableDataInfoInvention getInventoryList(@RequestParam("warehouseId") String warehouseId,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                                   @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
        return inventoryService.getInventoryList(warehouseId, pageSize, pageNum);
    }

    /**
     * 保存库存数据
     *
     * @param warehouseId 仓库主键
     * @return 保存结果
     */
    @PostMapping("/saveInventoryData/{warehouseId}")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:insert')")
    public AjaxResult saveInventoryData(@PathVariable("warehouseId") String warehouseId) {
        if (warehouseId == null || warehouseId.isEmpty()) {
            return AjaxResult.error("请选择需要进行同步的仓库！");
        }
        int i = inventoryService.saveInventoryData(warehouseId);
        return toAjax(i);
    }

    /**
     * 手动比对数据
     *
     * @param warehouseId 仓库id
     * @return 同步结果
     */
    @PostMapping("/manualSyncing/{warehouseId}")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:handle')")
    public AjaxResult manualSyncing(@PathVariable("warehouseId") String[] warehouseId) {
        return inventoryService.manualSyncingTheOnHandQuantity(warehouseId);
    }

    /**
     * 获取库存比对任务列表
     *
     * @param warehouseId 仓库主键
     * @param compareTime 比对时间
     * @param isResolved  是否已解决
     * @return 库存数据
     */
    @GetMapping("/abnormalInventory")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:list')")
    public TableDataInfo getInventoryData(@RequestParam(name = "warehouseId", required = false) String warehouseId,
                                          @RequestParam(name = "compareTime", required = false) String compareTime,
                                          @RequestParam(name = "isResolved", required = false, defaultValue = "0") String isResolved,
                                          @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        // 获取时间范围
        BasicQueryDTO basicQueryDTO = new BasicQueryDTO();
        if (compareTime != null && !compareTime.isEmpty()) {
            basicQueryDTO.setDbilldate(compareTime);
            DataProcessUtils.handleDateRange(basicQueryDTO);
        }
        return inventoryService.abnormalInventory(warehouseId, basicQueryDTO.getStartDate(),
                basicQueryDTO.getEndDate(), isResolved, pageNum, pageSize);
    }

    /**
     * 获取比对任务库存异常详情
     *
     * @param taskId 比对任务主键
     * @return 异常数据
     */
    @GetMapping("/getInventoryDiscrepancyDetail")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:list')")
    public TableDataInfo getInventoryDiscrepancyDetail(Long taskId) {
        startPage();
        List<InventoryDiscrepancyDetail> inventoryDiscrepancyDetailList = inventoryService.getInventoryDiscrepancyDetail(taskId);
        return getDataTable(inventoryDiscrepancyDetailList);
    }

    /**
     * 更新异常库存处理状态
     *
     * @param taskId 库存比对任务id
     * @return 操作结果
     */
    @PostMapping("/{taskId}")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:update')")
    public AjaxResult updateTaskId(@PathVariable("taskId") Long taskId) {
        int i = inventoryService.updateTaskId(taskId);
        return toAjax(i);
    }

    /**
     * 导出库存异常数据
     *
     * @param ids 库存异常id
     */
    @PostMapping("/export/{ids}")
    @PreAuthorize("@ss.hasPermi('inventory:comparison:export')")
    public void export(HttpServletResponse response, @PathVariable("ids") Long[] ids) {
        List<InventoryDiscrepancyDetail> list = inventoryService.inventoryDiscrepancyDetailList(ids);
        ExcelUtil<InventoryDiscrepancyDetail> util = new ExcelUtil<>(InventoryDiscrepancyDetail.class);
        util.exportExcel(response, list, "库存异常记录");
    }

    /**
     * 获取是否存在没有处理的库存异常
     *
     * @return 查询结果
     */
    @GetMapping("/isResolved")
    public AjaxResult isResolved() {
        int i = inventoryService.selectIsResolved();
        return AjaxResult.success(i);
    }
}
